/**
 * $Id$
 *
 * Copyright (c) 2014 ChinaSoft International, Ltd. All rights reserved
 * JointForce Project
 *
 */
package com.csi.jf.crucible.api.crucible;

import java.io.IOException;

import com.icss.j2ee.util.http.HttpClientException;

/**
 * @Title: ApiTestGroups.java
 * @Description: <br>
 *               <br>
 * @Company: Chinasofti
 * @Created on 2014-7-4 下午3:24:50
 * @author zhangmh
 * @version $Revision: 1.0 $
 * @since 1.0
 */
public class ApiTestGroups {
    
    private RestClient client = new RestClient();
    private static String newGroupName = "team-6";
    
    public static void main(String[] args) throws Exception{
        ApiTestGroups test = new ApiTestGroups();
        test.adminGroups();
    }
    
    /**
    @param type string filter groups by authentication type: built-in, LDAP, host, custom, ajp, crowd, ...
    @param admin boolean filter groups by admin flag
    @param prefix string filter groups by name prefix
    @return
    {
       "start": 0, 
       "limit": 100, 
       "lastPage": true, 
       "size": 1, 
       "values": [
           {
               "name": "crucible-administrators", 
               "types": [
                   "crowd"
               ], 
               "admin": true
           }
       ]
    }
    @return
    {
       "start": 0, 
       "limit": 100, 
       "lastPage": true, 
       "size": 10, 
       "values": [
           {
               "name": "confluence-administrators", 
               "types": [
                   "crowd"
               ], 
               "admin": false
           }, 
           {
               "name": "confluence-users", 
               "types": [
                   "crowd"
               ], 
               "admin": false
           }, 
           {
               "name": "crowd-administrators", 
               "types": [
                   "crowd"
               ], 
               "admin": false
           }, 
           {
               "name": "crowd-users", 
               "types": [
                   "crowd"
               ], 
               "admin": false
           }, 
           {
               "name": "crucible-users", 
               "types": [
                   "crowd"
               ], 
               "admin": false
           }, 
           {
               "name": "jira-administrators", 
               "types": [
                   "crowd"
               ], 
               "admin": false
           }, 
           {
               "name": "jira-developers", 
               "types": [
                   "crowd"
               ], 
               "admin": false
           }, 
           {
               "name": "jira-users", 
               "types": [
                   "crowd"
               ], 
               "admin": false
           }, 
           {
               "name": "sonar-administrators", 
               "types": [
                   "crowd"
               ], 
               "admin": false
           }, 
           {
               "name": "sonar-users", 
               "types": [
                   "crowd"
               ], 
               "admin": false
           }
       ]
   }
    * @throws Exception
    */
   public void adminGroups()throws Exception{
       //Retrieve a page of groups.
       String url = "/rest-service-fecru/admin/groups?type=crowd&admin=false&prefix=";
       client.doGet(url);
   }
   
   public void testCreateGroup() throws HttpClientException, IOException{
     //Creates a new built-in group.
       String url = "/rest-service-fecru/admin/groups";
       String json = "{\"name\" : \""+newGroupName+"\",\"types\" : [ \"built-in\" ],\"admin\" : true}";
       client.doPost(url,json);
   }
   
   public void testRetrieveeGroup() throws HttpClientException, IOException{
       //Retrieve a group by name.
       //{"name":"crucible-users","types":["built-in","crowd"],"admin":false}
       String url = "/rest-service-fecru/admin/groups/"+newGroupName;
       client.doGet(url);
   }
   
   public void testUpdateGroup() throws HttpClientException, IOException{
       //Updates an existing group.
       String url = "/rest-service-fecru/admin/groups/"+newGroupName;
       String json = "{\"admin\" : false}";
       client.doPut(url,json);
   }
   
   public void testDeleteGroup() throws HttpClientException, IOException{
       //Deletes a group by name
       String url = "/rest-service-fecru/admin/groups/"+newGroupName;
       client.doDelete(url);
   }

}
